package main

import (
	"flag"
	"ginfast/pkg/templ"
	"os"
	"path/filepath"
	"strings"
)

var (
	service  = flag.String("s", "generator", "请输入所属服务")
	model    = flag.String("m", "Code", "请输入Model模型")
	filename = flag.String("f", "code.go", "文件名")
)

func genCode(filename string, codeHandle func() string) error {
	os.MkdirAll(filepath.Dir(filename), os.ModePerm)
	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	templstr := codeHandle()
	file.WriteString(templstr)
	file.Close()
	return nil
}

func main() {
	flag.Parse()
	genCode("../../api/"+*service+"/controller/"+*filename, func() string {
		templstr := templ.Controller
		templstr = strings.ReplaceAll(templstr, "{{.Service}}", *service)
		templstr = strings.ReplaceAll(templstr, "{{.Model}}", *model)
		return templstr
	})
	genCode("../../api/"+*service+"/model/"+*filename, func() string {
		templstr := templ.Model
		templstr = strings.ReplaceAll(templstr, "{{.Model}}", *model)
		return templstr
	})
}
